﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;
using System.Web.Mvc;

namespace BlackBird.DataAccess
{
	/// <summary>
	/// 用来做测试
	/// </summary>
	public static class MockupDataStore
	{
        #region Bijiadan
        //private static Dictionary<String, BiJiaDanDM> bijiadanSave = new Dictionary<String, BiJiaDanDM>();
        //public static string CreatBiJiaDan(BiJiaDanDM bijiadan)
        //{
        //    bijiadan.Id = Guid.NewGuid().ToString();
        //    bijiadanSave.Add(bijiadan.Id,bijiadan);
        //    return bijiadan.Id;
        //}

        //public static void UpdateBiJiaDan(BiJiaDanDM dm)
        //{
        //    bijiadanSave[dm.Id] = dm;
        //}

        //public static BiJiaDanDM GetBiJiaDan(string ID)
        //{
        //    return bijiadanSave.SingleOrDefault(v => v.Key == ID).Value;
        //}

        //public static BiJiaDanTableDM CreateDefaultTable(int nCompayCount, int nProductCount)
        //{
        //    BiJiaDanTableDM oT = new BiJiaDanTableDM();
        //    oT.Companies = new List<SupplierDM>();
        //    for (int index = 0; index < nCompayCount; index++)
        //    {
        //        var supplider = new SupplierDM()
        //        {
        //            Name = "myCompany" + index.ToString(),
        //            Contacter = "myContacter" + index.ToString(),
        //            CallNumber = "myCallNumber" + index.ToString()
        //        };
        //        oT.Companies.Add(supplider);

        //        MockupDataStore.AddSupplier(supplider);
        //    }
        //    oT.ProductRows = new List<OrderTableRowDM>();

        //    for (int index = 0; index < nProductCount; index++)
        //    {
        //        var productRow = new OrderTableRowDM();
        //        productRow.XuHao = index + 1;
        //        productRow.Product = new ProductDM()
        //        {
        //            Name = "good" + index.ToString(),
        //            Unit = "个"
        //        };

        //        MockupDataStore.AddProduct(productRow.Product);

        //        productRow.SupplierDeclarePrices = new List<SupplierDeclarePriceDM>();

        //        for (int y = 0; y < nCompayCount; y++)
        //        {
        //            productRow.SupplierDeclarePrices.Add(new SupplierDeclarePriceDM() { DeclarePrice = float.Parse(index.ToString() + "." + y.ToString()) });
        //        }
        //        oT.ProductRows.Add(productRow);
        //    }

        //    return oT;
        //}
        #endregion

        #region Project
        //private static AllProjectsDM all_projects = new AllProjectsDM();

        //public static AllProjectsDM GetAllProjects()
        //{
        //    if (all_projects.AllProjects.Count > 0)
        //    {
        //        return all_projects;
        //    }

        //    all_projects.AllProjects.Add(BuildMockUpProject("上海徐汇闵行漕河泾开发区"));
        //    all_projects.AllProjects.Add(BuildMockUpProject("上海徐汇闵行漕河泾开发区"));
        //    all_projects.AllProjects.Add(BuildMockUpProject("上海徐汇闵行漕河泾开发区"));

        //    return all_projects;
        //}

        //public static ProjectDM NewProject(NewProjectDM dm)
        //{
        //    var p = BuildMockUpProject(dm.Name);
        //    all_projects.AllProjects.Add(p);
        //    return p;
        //}

        //public static ProjectDM BuildMockUpProject(string name)
        //{
        //    return new ProjectDM()
        //    {
        //        thumbnialurl = Links.Content.Images.project.example_PNG,
        //        Id = Guid.NewGuid().ToString(),
        //        BasicInfo = new ProjectBasicDM()
        //        {
        //            Name = name,
        //            Status = "初始状态",
        //            CreateDate = DateTime.Today.ToString("yyyy-MM-dd"),
        //            CreateUserName = "XXX",
        //            IncomeCount = 3,
        //            ExpendCount = 4,
        //        }
        //    };
        //}

        //public static ProjectDM GetProject(string id)
        //{
        //    return all_projects.AllProjects.SingleOrDefault(v => v.Id == id);
        //}

        //public static void AddBijiaDanToProject(string projectId, string bijiadanId)
        //{
        //    ProjectDM parent = GetProject(projectId);
        //    parent.BasicInfo.BiJiaDanCount++;

        //    //TODO， need to hookup the bijiadan and project
        //    //Project needs has a member to keep bijiadan id
        //}
        #endregion

        #region Item List (like Bijiandan, Income lists)
        //public static ItemListViewDM GetItemListViewDM()
        //{
        //    ItemListViewDM lv = new ItemListViewDM();

        //    //add bijiadan to list view
        //    foreach (var p in BiJiaDanDataAccess.BiJiadanDictionary)
        //    {
        //        ItemInfoDM info = new ItemInfoDM
        //        {
        //            Id = p.Key,
        //            Name = "比价单",
        //            CreateOwn = "xxx",
        //            CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
        //            type = SupportTypes.BiJiaDan
        //        };

        //        if (p.Value.Status == BiJiaDanStatus.ShengHe)
        //            info.status = SupportTypeStatus.DaiShenHe;
        //        else if (p.Value.Status == BiJiaDanStatus.Complete)
        //            info.status = SupportTypeStatus.GuiDang;
        //        else
        //            info.status = SupportTypeStatus.JiHua;

        //        lv.Items.Add(info);
        //    }

        //    //lv.Items.Add(GetItemInfoDM(SupportTypes.BiJiaDan, SupportTypeStatus.DaiShenHe));
        //    //lv.Items.Add(GetItemInfoDM(SupportTypes.BiJiaDan, SupportTypeStatus.GuiDang));
        //    //lv.Items.Add(GetItemInfoDM(SupportTypes.BiJiaDan, SupportTypeStatus.GuiDang));

        //    //lv.Items.Add(GetItemInfoDM(SupportTypes.BiJiaDan, SupportTypeStatus.DaiShenHe));
        //    //lv.Items.Add(GetItemInfoDM(SupportTypes.BiJiaDan, SupportTypeStatus.DaiShenHe));

        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Income, SupportTypeStatus.DaiShenHe));
        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Income, SupportTypeStatus.DaiShenHe));

        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Income, SupportTypeStatus.GuiDang));
        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Income, SupportTypeStatus.GuiDang));
        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Income, SupportTypeStatus.GuiDang));

        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Expend, SupportTypeStatus.GuiDang));
        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Expend, SupportTypeStatus.GuiDang));

        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Expend, SupportTypeStatus.DaiShenHe));
        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Expend, SupportTypeStatus.DaiShenHe));
        //    lv.Items.Add(GetItemInfoDM(SupportTypes.Expend, SupportTypeStatus.DaiShenHe));

        //    return lv;
        //}

        //public static ItemInfoDM GetItemInfoDM(SupportTypes type,SupportTypeStatus status)
        //{
        //    return new ItemInfoDM
        //    {
        //        Id = Guid.NewGuid().ToString(),
        //        Name = "Name",
        //        CreateOwn = "xxx",
        //        CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
        //        type = type,
        //        status = status
        //    };
        //}

        //public static IncomeAndExpendInputDM GetIncomeAndExpendById(String id)
        //{
        //    return new IncomeAndExpendInputDM
        //    {
        //        Id = id,
        //        Name = "my Test",
        //        Count = 10000,
        //        Description = "this is just used for testing the conception"
        //    };
        //}

        //public static ProjectEventDM GetProjectEvetnById(string id)
        //{
        //    return new ProjectEventDM
        //    {
        //        Id = id,
        //        Name = "Project Event",
        //        Description = "项目进行的非常顺利",
        //        ParentId = "000",
        //        type = SupportTypes.Event
        //    };
        //}

        //public static BiJiaDanDM GetBiJiaDanById(string id)
        //{
        //    return BiJiaDanDataAccess.GetBiJiaDan(id);
        //}

        #endregion

        #region Product
        //static List<ProductDM> all_products = new List<ProductDM>();
        //public static ICollection<ProductDM> AllProductsDM()
        //{
        //    if (all_products.Count > 0) return all_products;

        //    all_products.Add(new ProductDM(){ Name = "机器人"});
        //    all_products.Add(new ProductDM(){ Name = "流水线"});
        //    all_products.Add(new ProductDM() { Name = "数控机床" });

        //    return all_products;
        //}

        //public static void AddProduct(ProductDM dm)
        //{
        //    var findProduct = all_products.Find(p => p.Name.Equals(dm.Name));
        //    if (findProduct != null) return;

        //    all_products.Add(dm);
        //}
        #endregion

        #region Supplier
        //static List<SupplierDM> all_supplier = new List<SupplierDM>();
        //public static ICollection<SupplierDM> AllSuppliersDM()
        //{
        //    if (all_supplier.Count > 0) return all_supplier;

        //    all_supplier.Add(new SupplierDM() { Name = "Autodesk" });
        //    all_supplier.Add(new SupplierDM() { Name = "IBM" });
        //    all_supplier.Add(new SupplierDM() { Name = "Facebook" });

        //    return all_supplier;
        //}

        //public static void AddSupplier(SupplierDM dm)
        //{
        //    var findSupplier = all_supplier.Find(p => p.Name.Equals(dm.Name));
        //    if (findSupplier != null) return;

        //    all_supplier.Add(dm);
        //}
        
        #endregion

        #region Plan
        public static PlanViewItemDM GetEmptyPlanViewItemDM(string ParentId, string name = "购买飞机票", string sComment = "上海到成都", SupportTypes sType = SupportTypes.Expend)
        {
            PlanViewItemDM pDm = new PlanViewItemDM();
            pDm.Id = Guid.NewGuid().ToString();
            pDm.ParentId = ParentId;
            pDm.FinishTime = DateTime.Now;
            pDm.DropDownType = new List<SelectListItem>()
            {
                new SelectListItem(){
                    Text = SupportTypes.Income.PropertyToString(),
                    Value = SupportTypes.Income.ToString(),
                    Selected = true
                },
                new SelectListItem(){
                    Text = SupportTypes.Expend.PropertyToString(),
                    Value = SupportTypes.Expend.ToString(),
                    Selected = false
                },
                new SelectListItem(){
                    Text = SupportTypes.Event.PropertyToString(),
                    Value = SupportTypes.Event.ToString(),
                    Selected = true
                },
            };
            pDm.Name = name;
            pDm.type = sType;
            pDm.Comment = sComment;


            return pDm;
        }

        /// <summary>
        /// 返回一个空的审核对象来做审核表单的初始化对象
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static CommentDM GetEmptyCommentDM(string Id, SupportTypes type)
        {
            CommentDM dm = new CommentDM();
            dm.ID = Id;
            dm.ContextType = type;
            dm.DropDownItems = new List<SelectListItem>()
            {
                new SelectListItem(){
                    Text = "审核通过",
                    Value = ShenHeResult.Pass.ToString(),
                    Selected = true
                },
                new SelectListItem(){
                    Text = "不同意",
                    Value = ShenHeResult.Fail.ToString(),
                    Selected = false
                }
            };

            return dm;
        }

        public static Dictionary<string, List<PlanViewItemDM>> GetProjectPlanViewMockUpData(string ParentId)
		{
			Dictionary<string, List<PlanViewItemDM>> ret = new Dictionary<string, List<PlanViewItemDM>>();

			List<PlanViewItemDM> items1 = new List<PlanViewItemDM>();
			items1.Add(GetEmptyPlanViewItemDM(ParentId));
			ret.Add("2012-01-03",items1);

			List<PlanViewItemDM> items2 = new List<PlanViewItemDM>();
			items2.Add(GetEmptyPlanViewItemDM(ParentId, "收回尾款", "项目结束收回尾款", SupportTypes.Income));
			ret.Add("2012-01-05", items2);

			List<PlanViewItemDM> items4 = new List<PlanViewItemDM>();
			items4.Add(GetEmptyPlanViewItemDM(ParentId, "收回尾款", "项目结束收回尾款", SupportTypes.Income));
			items4.Add(GetEmptyPlanViewItemDM(ParentId, "付项目款", "项目结束收回尾款", SupportTypes.Expend));
			items4.Add(GetEmptyPlanViewItemDM(ParentId, "项目收尾", "按照计划顺利完成", SupportTypes.Event));
			ret.Add("2012-03-05", items4);

			List<PlanViewItemDM> items3 = new List<PlanViewItemDM>();
			items3.Add(GetEmptyPlanViewItemDM(ParentId, "收回尾款", "项目结束收回尾款", SupportTypes.Income));
			items3.Add(GetEmptyPlanViewItemDM(ParentId, "项目收尾", "按照计划顺利完成", SupportTypes.Event));
			ret.Add("2012-02-05", items3);

			//添加今天作为时间轴
			if (!ret.ContainsKey(DateTime.Now.ToString("yyyy-MM-dd")))
			{
				List<PlanViewItemDM> itemstoday = new List<PlanViewItemDM>();
				ret.Add(DateTime.Now.ToString("yyyy-MM-dd"), itemstoday);
			}

			var orderEnumer = ret.OrderBy(v => v.Key, new PlanDateCompare());

			return orderEnumer.ToDictionary<KeyValuePair<string, List<PlanViewItemDM>>, string, List<PlanViewItemDM>>(p => p.Key, p => p.Value);

		}

		public class PlanDateCompare : IComparer<string>
		{
			// Because the class implements IComparer, it must define a 
			// Compare method. The method returns a signed integer that indicates 
			// whether s1 > s2 (return is greater than 0), s1 < s2 (return is negative),
			// or s1 equals s2 (return value is 0). This Compare method compares strings. 
			public int Compare(string s1, string s2)
			{
				DateTime dt1 = DateTime.Parse(s1);
				DateTime dt2 = DateTime.Parse(s2);
				return DateTime.Compare(dt1, dt2);
			}
        }
        #endregion

        #region Users
        static List<UserDM> usersForTest = new List<UserDM>();

        public static ICollection<UserDM> GetAllUsers()
        {
            if ( usersForTest.Count > 0) return usersForTest;

            usersForTest.Add(new UserDM { ID = 1, 
                Name = "admin", 
                Password = "admin", 
                Roles = new[] { RoleType.Admin } 
            });

            usersForTest.Add(new UserDM
            {
                ID = 2,
                Name = "caigou",
                Password = "caigou",
                Roles = new[] { RoleType.CaiGou }
            });

            usersForTest.Add(new UserDM
            {
                ID = 3,
                Name = "shenhe",
                Password = "shenhe",
                Roles = new[] { RoleType.ShenHe }
            });
           
            return usersForTest;
        }
        #endregion

		#region 合同

		public static List<AttachedFile> GetAttachedFileList()
		{
			List<AttachedFile> attachedFiles = new List<AttachedFile>();

			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());
			attachedFiles.Add(CreateSampleAttachedFile());


			return attachedFiles;
		}

		public static AttachedFile CreateSampleAttachedFile()
		{
			return new AttachedFile
			{
				ID = Guid.NewGuid().ToString(),
				FileName = "My test file :" + Guid.NewGuid().ToString(),
				UploadDate = DateTime.Now.ToString("yyyy-MM-dd"),
				UploadUserName = "Chen"
			};
		}

		#endregion
	}
}